import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jc.common.OrderNumberGenerator;
import com.jc.constant.CountryEnum;
import com.jc.util.HttpPostUtil;
import com.jc.util.R;
import com.jc.vo.ApiConfig;
import com.jc.vo.CreditSaleVO;
import com.jc.vo.QuotaApplyVO;
import org.junit.Test;

import java.io.IOException;
import java.math.BigDecimal;
import java.util.Date;

public class CreditSaleDemo {


    // jc提供的SecretId
    public static String SECRET_ID = "100001";
    // jc提供的SecretKey
    public static String SECRET_KEY = "a28aa7399a067919c913ead1b434552c";


    public static String SERVICE_HOST = "127.0.0.1:8080";

    public static String QUOTA_APPLY_URL = "http://" + SERVICE_HOST + "/jc-order/creditSale/v1/";


    @Test
    public void testCreate() {
        String m="declareData";
        ApiConfig config = new ApiConfig(SECRET_ID, SECRET_KEY, QUOTA_APPLY_URL+"declareData");
        CreditSaleVO vo = getVo("OA");
        vo.setOperationType("MODIFY");
        vo.setCorpSerialNo("DI20250718154353001");
        this.send(config, vo);
    }



    private static CreditSaleVO getVo(String contractPayment) {

        CreditSaleVO vo = new CreditSaleVO();
        vo.setCreateUser("张三");
        vo.setOperationType("NEW");

//        vo.setCorpSerialNo(OrderNumberGenerator.generate("CI"));

        vo.setHappenDate(new Date());

        vo.setRiskPiccCode("PICC-000001-011");
        vo.setStartDate(new Date());

        vo.setContractPayment(contractPayment);

        vo.setCurrency("USD");
        vo.setCountryOrArea(CountryEnum.USA.getCode());
        vo.setCustomsCode("9801009000");
        vo.setXamInvoiceAmout(new BigDecimal("3000"));
        vo.setPaymentTerms(120);
        vo.setContractNo(OrderNumberGenerator.generate("TEST"));

        if ("LC".equalsIgnoreCase(vo.getContractPayment()) || "LCOA".equalsIgnoreCase(vo.getContractPayment())) {
            vo.setBankSwift("TEST3020323");
            vo.setComitFormDate(new Date());
        }

        return vo;
    }


    private void send(ApiConfig config, CreditSaleVO vo) {
        try {
            ObjectMapper om = new ObjectMapper();
            om.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
            String response = HttpPostUtil.send(om.writeValueAsString(vo), config, false);
            R r = om.readValue(response, R.class);
            try {
                if (r.getSuccess()) {
                    QuotaApplyVO result = om.convertValue(r.getContent(), QuotaApplyVO.class);
                    r.setContent(result);
                }
            } catch (Exception e) {
                r.setSuccess(false);
                r.setCode(R.RCODE.SYS_ERROR.getCode());
                r.setMessage(R.RCODE.SYS_ERROR.getMessage());
                e.printStackTrace();
            }
            System.out.println(r.getMessage());
            System.out.println(response);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
